package com.anosine.gpsl.android.widget.wheelview;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import com.anosine.gpsl.android.R;
/**
 * @Title: MyDateTimePickerDlg
 * @Package com.anosine.gpsl.android.widget.wheelview
 * @Description: 
 * @author: sunny
 * @date: 2015-11-24
 * @version V1.0
 */
public class MyDateTimePickerDlg extends DialogFragment implements OnClickListener {
    private static OnDateTimeSetListener mCallBack;

    private static int mDefaultStartYear = 2000;
    private static int mDefaultEndYear = 2020;
    private boolean mIsShowHour=true;
    private boolean mIsShowMinute=true;
    private boolean mIsSetTimeSelf=false;
    private Calendar mCalendar;
    private int curr_year, curr_month, curr_day, curr_hour, curr_minute;
    // 添加大小月月份并将其转换为list,方便之后的判断
    private String[] months_big = {"1", "3", "5", "7", "8", "10", "12"};
    private String[] months_little = {"4", "6", "9", "11"};
    private WheelView mWvYear, mWvMonth, mWvDay, mWvHours, mWvMins;
    private List<String> list_big, list_little;

    private static final String Tag_Bundle_Key_Hour="Tag_Bundle_Key_Hour";
    private static final String Tag_Bundle_Key_Minute="Tag_Bundle_Key_Minute";
    private static final String Tag_Bundle_Key_Start_Time="Tag_Bundle_Key_Start_Time";
    private static final String Tag_Bundle_Key_End_Time="Tag_Bundle_Key_End_Time";

    private static final String Tag_Bundle_Key_Is_Time_Self_Set="Tag_Bundle_Key_Is_Time_Self_Set";
    private static final String Tag_Bundle_Key_Is_Time_Self_Set_Year="Tag_Bundle_Key_Is_Time_Self_Set_Year";
    private static final String Tag_Bundle_Key_Is_Time_Self_Set_Month="Tag_Bundle_Key_Is_Time_Self_Set_Month";
    private static final String Tag_Bundle_Key_Is_Time_Self_Set_Day="Tag_Bundle_Key_Is_Time_Self_Set_Day";
    private static final String Tag_Bundle_Key_Is_Time_Self_Set_Hour="Tag_Bundle_Key_Is_Time_Self_Set_Hour";
    private static final String Tag_Bundle_Key_Is_Time_Self_Set_Minute="Tag_Bundle_Key_Is_Time_Self_Set_Minute";

    public static MyDateTimePickerDlg instance(){
        return instance(true, true, mDefaultStartYear, mDefaultEndYear);
    }

    public static MyDateTimePickerDlg instance(boolean isShowHour, boolean isShowMinute){
        return instance(isShowHour, isShowMinute, mDefaultStartYear, mDefaultEndYear);
    }

    public static MyDateTimePickerDlg instance(boolean isShowHour, boolean isShowMinute, int startTime, int endTime){
        MyDateTimePickerDlg dlg=new MyDateTimePickerDlg();
        Bundle args=new Bundle();
        args.putBoolean(Tag_Bundle_Key_Is_Time_Self_Set, false);
        args.putBoolean(Tag_Bundle_Key_Hour, isShowHour);
        args.putBoolean(Tag_Bundle_Key_Minute, isShowMinute);
        args.putInt(Tag_Bundle_Key_Start_Time, startTime);
        args.putInt(Tag_Bundle_Key_End_Time, endTime);
        dlg.setArguments(args);
        dlg.setArguments(args);
        return dlg;
    }

    public static MyDateTimePickerDlg instance(boolean isSetTimeSelf, int startTime, int endTime, int year, int month, int day, int hour, int minute){
        MyDateTimePickerDlg dlg=new MyDateTimePickerDlg();
        Bundle args=new Bundle();
        args.putBoolean(Tag_Bundle_Key_Hour, true);
        args.putBoolean(Tag_Bundle_Key_Minute, true);
        args.putBoolean(Tag_Bundle_Key_Is_Time_Self_Set, isSetTimeSelf);

        args.putInt(Tag_Bundle_Key_Is_Time_Self_Set_Year, year);
        args.putInt(Tag_Bundle_Key_Is_Time_Self_Set_Month, month);
        args.putInt(Tag_Bundle_Key_Is_Time_Self_Set_Day, day);
        args.putInt(Tag_Bundle_Key_Is_Time_Self_Set_Hour, hour);
        args.putInt(Tag_Bundle_Key_Is_Time_Self_Set_Minute, minute);

        args.putInt(Tag_Bundle_Key_Start_Time, startTime);
        args.putInt(Tag_Bundle_Key_End_Time, endTime);
        dlg.setArguments(args);
        dlg.setArguments(args);
        return dlg;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(Tag_Bundle_Key_Is_Time_Self_Set, mIsSetTimeSelf);
        outState.putBoolean(Tag_Bundle_Key_Hour, mIsShowHour);
        outState.putBoolean(Tag_Bundle_Key_Minute, mIsShowMinute);
        outState.putInt(Tag_Bundle_Key_Start_Time, mDefaultStartYear);
        outState.putInt(Tag_Bundle_Key_End_Time, mDefaultEndYear);
    }

    public static void setmCallBack(OnDateTimeSetListener mCallBack) {
        MyDateTimePickerDlg.mCallBack = mCallBack;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.custom_dlg);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle args=getArguments()!=null? getArguments(): savedInstanceState;
        if (args!=null){
            mIsShowHour=args.getBoolean(Tag_Bundle_Key_Hour);
            mIsShowMinute=args.getBoolean(Tag_Bundle_Key_Minute);
            mDefaultStartYear=args.getInt(Tag_Bundle_Key_Start_Time);
            mDefaultEndYear=args.getInt(Tag_Bundle_Key_End_Time);

            mIsSetTimeSelf=args.getBoolean(Tag_Bundle_Key_Is_Time_Self_Set);
        }

        View view = inflater.inflate(R.layout.show_time_select_dlg, null);

        int year =0;
        int month =0;
        int day =0;
        int hour =0;
        int minute =0;
        if (!mIsSetTimeSelf){
            mCalendar = Calendar.getInstance();
            year = mCalendar.get(Calendar.YEAR);
            month = mCalendar.get(Calendar.MONTH);
            day = mCalendar.get(Calendar.DATE);
            hour = mCalendar.get(Calendar.HOUR_OF_DAY);
            minute = mCalendar.get(Calendar.MINUTE);
        }else{
            if (args!=null){
                year=args.getInt(Tag_Bundle_Key_Is_Time_Self_Set_Year);
                month=args.getInt(Tag_Bundle_Key_Is_Time_Self_Set_Month)-1;
                if (month<0){
                    month=12;
                }
                day=args.getInt(Tag_Bundle_Key_Is_Time_Self_Set_Day);
                hour=args.getInt(Tag_Bundle_Key_Is_Time_Self_Set_Hour);
                minute=args.getInt(Tag_Bundle_Key_Is_Time_Self_Set_Minute);
            }
        }

        list_big = Arrays.asList(months_big);
        list_little = Arrays.asList(months_little);
        Button btnOk = (Button) view.findViewById(R.id.btn_time_ok);
        Button btnTimeCancle = (Button) view
                .findViewById(R.id.btn_time_cancle);
        btnOk.setOnClickListener(this);
        btnTimeCancle.setOnClickListener(this);
        int textSize = getResources().getDimensionPixelSize(R.dimen.h3);
        // 年
        mWvYear = (WheelView) view.findViewById(R.id.year);
        mWvYear.setAdapter(new NumericWheelAdapter(mDefaultStartYear, mDefaultEndYear));// 设置"年"的显示数据
        mWvYear.setCyclic(true);// 可循环滚动
        mWvYear.setLabel("年");// 添加文字
        mWvYear.setCurrentItem(year - mDefaultStartYear);// 初始化时显示的数据

        // 月
        mWvMonth = (WheelView) view.findViewById(R.id.month);
        mWvMonth.setAdapter(new NumericWheelAdapter(1, 12));
        mWvMonth.setCyclic(true);
        mWvMonth.setLabel("月");
        mWvMonth.setCurrentItem(month);

        // 日
        mWvDay = (WheelView) view.findViewById(R.id.day);
        mWvDay.setCyclic(true);
        // 判断大小月及是否闰年,用来确定"日"的数据
        if (list_big.contains(String.valueOf(month + 1))) {
            mWvDay.setAdapter(new NumericWheelAdapter(1, 31));
        } else if (list_little.contains(String.valueOf(month + 1))) {
            mWvDay.setAdapter(new NumericWheelAdapter(1, 30));
        } else {
            // 闰年
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                mWvDay.setAdapter(new NumericWheelAdapter(1, 29));
            else
                mWvDay.setAdapter(new NumericWheelAdapter(1, 28));
        }
        mWvDay.setLabel("日");
        mWvDay.setCurrentItem(day - 1);

        // 时
        mWvHours = (WheelView) view.findViewById(R.id.hour);
        mWvHours.setAdapter(new NumericWheelAdapter(0, 23));
        mWvHours.setCyclic(true);
        mWvHours.setCurrentItem(hour);

        // 分
        mWvMins = (WheelView) view.findViewById(R.id.mins);
        mWvMins.setAdapter(new NumericWheelAdapter(0, 59, "%02d"));
        mWvMins.setCyclic(true);
        mWvMins.setCurrentItem(minute);

        if (!mIsShowHour){
            mWvHours.setVisibility(View.GONE);
        }
        if (!mIsShowMinute){
            mWvMins.setVisibility(View.GONE);
        }


        // 添加"年"监听
        OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                int year_num = newValue + mDefaultStartYear;
                // 判断大小月及是否闰年,用来确定"日"的数据
                if (list_big
                        .contains(String.valueOf(mWvMonth.getCurrentItem() + 1))) {
                    mWvDay.setAdapter(new NumericWheelAdapter(1, 31));
                } else if (list_little.contains(String.valueOf(mWvMonth
                        .getCurrentItem() + 1))) {
                    mWvDay.setAdapter(new NumericWheelAdapter(1, 30));
                } else {
                    if ((year_num % 4 == 0 && year_num % 100 != 0)
                            || year_num % 400 == 0)
                        mWvDay.setAdapter(new NumericWheelAdapter(1, 29));
                    else
                        mWvDay.setAdapter(new NumericWheelAdapter(1, 28));
                }
            }
        };
        // 添加"月"监听
        OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                int month_num = newValue + 1;
                // 判断大小月及是否闰年,用来确定"日"的数据
                if (list_big.contains(String.valueOf(month_num))) {
                    mWvDay.setAdapter(new NumericWheelAdapter(1, 31));
                } else if (list_little.contains(String.valueOf(month_num))) {
                    mWvDay.setAdapter(new NumericWheelAdapter(1, 30));
                } else {
                    if (((mWvYear.getCurrentItem() + mDefaultStartYear) % 4 == 0 && (mWvYear
                            .getCurrentItem() + mDefaultStartYear) % 100 != 0)
                            || (mWvYear.getCurrentItem() + mDefaultStartYear) % 400 == 0)
                        mWvDay.setAdapter(new NumericWheelAdapter(1, 29));
                    else
                        mWvDay.setAdapter(new NumericWheelAdapter(1, 28));
                }
            }
        };
        mWvYear.addChangingListener(wheelListener_year);
        mWvMonth.addChangingListener(wheelListener_month);
        mWvDay.TEXT_SIZE = textSize;
        mWvHours.TEXT_SIZE = textSize;
        mWvMins.TEXT_SIZE = textSize;
        mWvMonth.TEXT_SIZE = textSize;
        mWvYear.TEXT_SIZE = textSize;

        return view;
    }

    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);
        WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();

        int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        lp.width =(int) (0.9f*width);
        getDialog().setCanceledOnTouchOutside(false);
    }

    public interface OnDateTimeSetListener {
        void onDateTimeSet(int year, int monthOfYear, int dayOfMonth, int hour, int minute);

        void onDateTimeCancel();
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.btn_time_ok:
                curr_year = mWvYear.getCurrentItem() + mDefaultStartYear;
                curr_month = mWvMonth.getCurrentItem() + 1;
                curr_day = mWvDay.getCurrentItem() + 1;
                curr_hour = mWvHours.getCurrentItem();
                curr_minute = mWvMins.getCurrentItem();
                if (mCallBack != null) {
                    mCallBack.onDateTimeSet(curr_year, curr_month, curr_day, curr_hour, curr_minute);
                    this.dismiss();
                }
                break;
            case R.id.btn_time_cancle:
                if (mCallBack != null) {
                    mCallBack.onDateTimeCancel();
                    this.dismiss();
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        mCallBack=null;
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        mCallBack=null;
    }

}

